#include <vector>
#include <iostream>

#include "Util.H"
#include "IO.H"
#include "Image.H"
#include "eval_thresh.h"

using namespace std;
using namespace pip;

int main(int argc, char **argv)
{
	//see if arguments are passed
    if (argc < 4) {
	  error("", "Usage: evaluate -t REFERENCE_IMAGE -r RESULT_IMAGE [-thresh THRESHOLD=0.5 [-s INTERVALL_LOWER_LIMIT] ] \n"
      "  Evaluates the performance of the result, regarding the referene.\n"
	  "  If values are passed with -s and -thresh,\n between these values all integral values are used as threshold sequentially.\n The results with increasing accuracy are printed to screen.\n"
      );
 	  return 1;
    }
	//collect arguments
    vector<string> args(argv, argv+argc);
    string ref, res;
	float thres(0.5), lower_limit(0),upper_limit(0); // if -s argument is passed use limits for search in threshold parameter-space.
    //get operands
	getScalarArg(args, "-thresh", thres);
	upper_limit = thres;						// so later we can see ...
	lower_limit = thres;				 
    getScalarArg(args, "-t"     , ref);
    getScalarArg(args, "-r"     , res);
	getScalarArg(args, "-s"     , lower_limit); // ... if lower and upper limit are equal TO find out if -s parameter is passed
	// reserve space for images
	Image<unsigned char> ureference, uresult;   // for different datatypes - different allocations
	Image<short>		 sreference, sresult;
	Image<float>         freference, fresult;
	// -9999 so one can see if 'it' didn't work
	float max_acc  = -9999;   // first accuracy value(>=0) is larger than initial max_acc
	float acc  = -9999;
	float sens = -9999;
	float spec = -9999;
	float over = -9999;
	cout << " | " << " Threshold "  //print title of table
		 << " | " << " Accuracy  " 
		 << " | " << "Sensitivity" 
		 << " | " << "Specificity" 
		 << " | " << "  Overlap  " 
		 << " |\t " << endl; 

	if(lower_limit < upper_limit) thres = lower_limit; // if limits are different: start threshold at lower_limit

	do{					// at least once do check what input images are given
											// to have the import of the images in the while loop is unnecessary, i know.
		if       (importFile(ureference, ref) && importFile(uresult, res) && (uresult.dimension()==ureference.dimension())) {
			evaluate(ureference, uresult, acc, sens, spec, over, thres);
		}else if (importFile(ureference, ref) && importFile(sresult, res) && (sresult.dimension()==ureference.dimension())){
			evaluate(ureference, sresult, acc, sens, spec, over, thres);
		}else if (importFile(ureference, ref) && importFile(fresult, res) && (fresult.dimension()==ureference.dimension())){
			evaluate(ureference, fresult, acc, sens, spec, over, thres);
		}else if (importFile(sreference, ref) && importFile(uresult, res) && (uresult.dimension()==sreference.dimension())){
			evaluate(sreference, uresult, acc, sens, spec, over, thres);
		}else if (importFile(sreference, ref) && importFile(sresult, res) && (sresult.dimension()==sreference.dimension())){
			evaluate(sreference, sresult, acc, sens, spec, over, thres);
		}else if (importFile(sreference, ref) && importFile(fresult, res) && (fresult.dimension()==sreference.dimension())){
			evaluate(sreference, fresult, acc, sens, spec, over, thres);
		}else if (importFile(freference, ref) && importFile(uresult, res) && (uresult.dimension()==freference.dimension())){
			evaluate(freference, uresult, acc, sens, spec, over, thres);
		}else if (importFile(freference, ref) && importFile(sresult, res) && (sresult.dimension()==freference.dimension())){
			evaluate(freference, sresult, acc, sens, spec, over, thres);
		}else if (importFile(freference, ref) && importFile(fresult, res) && (fresult.dimension()==freference.dimension())){
			evaluate(freference, fresult, acc, sens, spec, over, thres);
		}else{
			error("", "The two Images are not compatible. Only use equal sized images of short, float or unsigned char values!\n");
 			return 2;
		}

		if(acc>max_acc){
			max_acc=acc;		// if the accurancy is better than the last print the results into table
		cout << "\n |\t " << thres 
			 << " |\t " << acc 
			 << " |\t " << sens 
			 << " |\t " << spec 
			 << " |\t " << over 
			 << " |\t " << endl;
		}
		else
		{
			cout << ".";        // if not let the user know that the program is still running
		}
		thres += 1;  
	}while(thres < upper_limit);  // if we did not yet reach the upper_limit run with increased threshold
  //system("pause");
}